from scrapy.spider import BaseSpider
from scrapy.selector import Selector 
from audiolib.items import AudiolibFileItem


class AudiolibFileSpider(BaseSpider):
	name = "audiofile"
	allowed_domains = ["www.thereminvox.com"]
	start_urls = [
	"http://www.thereminvox.com/filemanager/list/1/index.html",
	"http://www.thereminvox.com/filemanager/list/2/index.html",
	"http://www.thereminvox.com/filemanager/list/3/index.html",
	"http://www.thereminvox.com/filemanager/list/4/index.html",
	"http://www.thereminvox.com/filemanager/list/5/index.html",
	"http://www.thereminvox.com/filemanager/list/6/index.html",
	"http://www.thereminvox.com/filemanager/list/7/index.html",
	"http://www.thereminvox.com/filemanager/list/8/index.html",
	"http://www.thereminvox.com/filemanager/list/9/index.html",
	"http://www.thereminvox.com/filemanager/list/11/index.html",
	"http://www.thereminvox.com/filemanager/list/12/index.html",
	"http://www.thereminvox.com/filemanager/list/16/index.html",
	"http://www.thereminvox.com/filemanager/list/17/index.html",
	"http://www.thereminvox.com/filemanager/list/18/index.html",
	"http://www.thereminvox.com/filemanager/list/19/index.html"
	]

	def parse(self, response):
		sel = Selector(response)
		#### Title
		titles = sel.xpath("//tr/td[@class='bgdarksmall'][@valign='top'][1]/a/b/text()") + sel.xpath("//tr/td[@class='bglightsmall'][@valign='top'][1]/a/b/text()")
		items = []
		for title in titles:
			item = AudiolibFileItem()
			item['title'] = title.extract()
			items.append(item)

		###### artista
		#esempio di codice per merge completo
		#artisti_d = sel.xpath("//tr/td[@class='bgdarksmall'][@valign='top'][1]/a/../text()")
		#artisti_l = sel.xpath("//tr/td[@class='bglightsmall'][@valign='top'][1]/a/../text()")
		#artisti = []
		#while artisti_d or artisti_l:
		#		if artisti_d:
		#			artisti.append(artisti_d.pop(0))
		#		if artisti_l:
		#			artisti.append(artisti_l.pop(0))
		artisti = sel.xpath("//tr/td[@class='bgdarksmall'][@valign='top'][1]/a/../text()") + sel.xpath("//tr/td[@class='bglightsmall'][@valign='top'][1]/a/../text()")
		i = 0
		for artista in artisti:
			items[i]['artist'] = artista.extract()
			i = i + 1

	        ##### descrizione
		descs = sel.xpath("//tr/td[@class='bgdarksmall'][@colspan='5'][1]") + sel.xpath("//tr/td[@class='bglightsmall'][@colspan='5'][1]")
		i = 0
		for desc in descs:
			descline = ""
			ilines = desc.xpath('i/text()')
			for iline in ilines:
				descline = descline + iline.extract()
			items[i]['desc'] = descline
			i = i + 1
			#if ilines:
			#	items[i]['desc'] = ilines[0].extract()

		##### filename    
		filenames = sel.xpath("//tr/td[@class='bgdarksmall']/a/@href") + sel.xpath("//tr/td[@class='bglightsmall']/a/@href")
		i = 0
		for filename in filenames:
			items[i]['filename'] = filename.extract().split('/')[-1]
			i = i + 1

		#data upload
		upldates = sel.xpath("//tr/td[@class='bgdarksmall'][@valign='top'][2]/text()") + sel.xpath("//tr/td[@class='bglightsmall'][@valign='top'][2]/text()")
		i = 0
		for upldate in upldates:
			items[i]['upldate'] =  upldate.extract()
			i = i + 1 

		##### dimensioni
                sizes = sel.xpath("//tr/td[@class='bgdarksmall'][@valign='top'][3]/nobr/text()") + sel.xpath("//tr/td[@class='bglightsmall'][@valign='top'][3]/nobr/text()")
		i = 0
		for size in sizes:
			items[i]['size'] = size.extract()
			i = i + 1 

		##### proprietario
		owners = sel.xpath("//tr/td[@class='bgdarksmall'][@valign='top'][4]/text()") + sel.xpath("//tr/td[@class='bglightsmall'][@valign='top'][4]/text()")
		i = 0
       		for owner in owners:
			items[i]['owner'] = owner.extract()
			i = i + 1 

		##### legacy_id	
		filenames = sel.xpath("//tr/td[@class='bgdarksmall']/a/@href") + sel.xpath("//tr/td[@class='bglightsmall']/a/@href")
		i = 0
		for filename in filenames:
			items[i]['legacy_id'] = filename.extract().split('/')[-2]
			i = i + 1

		##### labels
		labels = sel.xpath("//a[@class='path'][position()>1]/text()")
		labels_list = []
		for label in labels:
			labels_list.append(label.extract())
		labels_str = ", ".join(labels_list)
		for item in items:
			item['labels'] = labels_str			

		return items
